<!DOCTYPE html>
<html lang="en">
<title>进店模式添加新轮播</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="banner-add-box col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>进店模式添加新轮播</h5>
            </div>
            <div class="ibox-content">
                <form method="post" id="uploadForm" name="fileinfo" class="form-horizontal" enctype="multipart/form-data">
                    <div class="form-group"><label class="col-sm-2 control-label">标题</label>
                        <div class="col-sm-10"><input type="text" name="title" id="title" class="form-control" placeholder="请输入标题"></div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">链接</label>
                        <div class="col-sm-10"><input type="url" name="previewUrl" id="previewUrl" class="form-control" placeholder="请输入链接"></div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">轮播图</label>
                        <div class="col-sm-10">
                            <label for="file" class="file-input">
                                <input type="file" name="file" id="file"/>
                                <svg width="20" height="20" viewBox="0 0 20 17">
                                    <path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/>
                                </svg>
                                <span>选择文件 ...</span>
                            </label>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <button type="button" onclick="javascript:window.location.href='to_operating_manage'" class="btn btn-default pull-left">取消修改</button>
                            <button type="button" id="add_operating" class="btn btn-success pull-right">确定</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $("#add_operating").click(function () {
            doUpload();
        });
    });

    function OssUpload(param, file, fileName, callBack) {
        var policyBase64 = Base64.encode(param.policy);
        var signature = param.signature.split(':')[1];
        var filePathName = param.filePath + "/" + param.fileName;
        var fileFullName = param.contentHostName + "/" + filePathName;
        var request = new FormData();
        request.append('OSSAccessKeyId', param.accessKeyId);
        request.append('policy', policyBase64);
        request.append('Signature', signature);
        request.append('key', filePathName);
        for (var i in param.metaDatas) {
            request.append(i, param.metaDatas[i]);
        }
        request.append('file', file);
        request.append('submit', "Upload to OSS");
        $.ajax({
            url: param.contentHostName,
            data: request,
            processData: false,
            cache: false,
            async: false,
            contentType: false,
            //关键是要设置contentType 为false，不然发出的请求头 没有boundary
            //该参数是让jQuery去判断contentType
            type: "POST",
            success: function (data, textStatus, request) {
                if (textStatus === "nocontent") {
                    callBack(fileFullName);
                    swal("success!", '', 'success')
                } else {
                    swal(textStatus, '', 'error')
                }
            }
        });
    }

    function doUpload() {
        var title = Trim($("#title").val(), 'g');
        var previewUrl = Trim($("#previewUrl").val(), 'g');
        if (title == '') {
            swal("标题不能为空!", '', 'error');
            $("#title").focus();
            return;
        }
        if (previewUrl == '') {
            swal("链接不能为空!", '', 'error');
            $("#previewUrl").focus();
            return;
        }
        var flag = false;
        $("#file").each(function () {
            if ($(this).val() == "") {
                swal("请上传图片！", '', 'error');
                flag = true;
            }
        });
        if (flag == false) {
            var url = 'operating_add_instore';
            var formData = new FormData($("#uploadForm")[0]);

            swal({
                title: '确认添加',
                text: "",
                type: 'question',
                showCancelButton: true,
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, 确认添加',
                cancelButtonText: 'No, 再次确认'
            }).then(function () {
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (returndata) {
                        if (returndata > 0) {
                            swal("添加成功!", "页面即将跳转,请稍后...", "success");
                            window.setTimeout(function () {
                                window.location.href = 'to_operating_manage?1=1#tab-5';
                            }, 1000)
                        }
                    },
                    error: function (returndata) {
                        swal(returndata, '', 'error');
                    }
                });
            }, function () {
            });
        }
    }


    function Trim(str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g")
            result = result.replace(/\s/g, "");
        return result;
    }
</script>

</@body.page>
</html>